package com.iot.manager.ui.activities;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.iot.manager.R;
import com.iot.manager.data.models.Device;
import com.iot.manager.network.ApiService;
import com.iot.manager.network.RetrofitClient;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.io.IOException;

public class AddDeviceActivity extends AppCompatActivity {

    private ApiService apiService;
    private TextInputEditText etDeviceName;
    private TextInputEditText etDeviceId;
    private TextInputEditText etDeviceType;
    private TextInputEditText etDevicePassword;
    private MaterialButton btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_device);

        // 初始化API服务
        apiService = RetrofitClient.getInstance().getApiService();

        // 初始化视图
        initViews();
    }

    private void initViews() {
        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(R.string.add_device_title);

        // 初始化控件
        etDeviceName = findViewById(R.id.etDeviceName);
        etDeviceId = findViewById(R.id.etDeviceId);
        etDeviceType = findViewById(R.id.etDeviceType);
        etDevicePassword = findViewById(R.id.etDevicePassword);
        btnAdd = findViewById(R.id.btnAddDevice);

        // 设置添加按钮点击事件
        btnAdd.setOnClickListener(v -> addDevice());
    }

    private void addDevice() {
        String deviceName = etDeviceName.getText().toString().trim();
        String deviceId = etDeviceId.getText().toString().trim();
        String deviceType = etDeviceType.getText().toString().trim();
        String devicePassword = etDevicePassword.getText().toString().trim();

        if (deviceName.isEmpty() || deviceId.isEmpty() || deviceType.isEmpty() || devicePassword.isEmpty()) {
            Toast.makeText(this, R.string.add_device_error_empty, Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建设备对象
        Device device = new Device();
        device.setName(deviceName);
        device.setDeviceId(deviceId);
        device.setType(deviceType);
        device.setPassword(devicePassword);

        // 添加设备
        apiService.createDevice(device).enqueue(new Callback<Device>() {
            @Override
            public void onResponse(Call<Device> call, Response<Device> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(AddDeviceActivity.this, R.string.add_device_success, Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    String errorMessage;
                    try {
                        errorMessage = response.errorBody() != null ? 
                            response.errorBody().string() : 
                            getString(R.string.add_device_error);
                    } catch (IOException e) {
                        errorMessage = getString(R.string.add_device_error);
                        Log.e("AddDeviceActivity", "Error reading error body", e);
                    }
                    Toast.makeText(AddDeviceActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Device> call, Throwable t) {
                Toast.makeText(AddDeviceActivity.this, R.string.network_error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 